# -*- coding: UTF-8 -*-
"""
@Filename:exerc3.py
@author:Cora
@time:2025-04-18
"""
import csv
import re

def main():
    data = {}
    headers = []
    # 读取CSV文件
    with open('xiaofei.csv', 'r', encoding='gbk') as f:
        reader = csv.reader(f)
        headers = next(reader)  # 获取列标题
        # 逐行处理数据
        for row in reader:
            province = row[0]
            values = {}
            # 遍历列标题（从第二列开始）
            for i in range(1, len(headers)):
                try:
                    values[headers[i]] = float(row[i])
                except (IndexError, ValueError):
                    values[headers[i]] = 0.0
            data[province] = values  # 将省份数据存入字典

    # 获取用户输入并分割
    user_input = input("请输入你要查询的城市和项目以逗号分隔）：").strip()
    parts = re.split(r'[,]+', user_input)
    if len(parts) < 2:
        print("输入格式错误！")
        return
    # 解析输入内容
    province, *items = parts

    # 验证省份
    if province not in data:
        print(f"省份 '{province}' 不存在")
        return

    # 查询并输出结果
    print(f"你输入的城市是：{province}")
    for item in items:
        if item not in headers[1:]:
            print(f"项目 '{item}' 不存在")
            continue
        print(f"{item}的消费金额为: {data[province][item]:.1f}")

if __name__ == '__main__':
    main()